package itsm.isperp.module.service.performance;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.module.entity.performance.PerPerformanceSettingUsers;
import itsm.isperp.module.repository.performance.PerPerformanceSettingUsersMapper;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2017-08-28 17:24
 */
@Service
public class PerPerformanceSettingUsersService extends
    BaseService<PerPerformanceSettingUsers> {

  @Autowired
  protected PerPerformanceSettingUsersMapper perPerformanceSettingUsersMapper;

  @Override
  public PerPerformanceSettingUsersMapper getRepository() {
    return this.perPerformanceSettingUsersMapper;
  }

  public void saveRelation(String settingId, String userName) {
    JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
    String sql = "delete from per_performance_setting_users where setting_id='"
        + settingId + "' and app_user_name='" + userName + "'";
    jdbcTemplate.execute(sql);
    PerPerformanceSettingUsers newOne = new PerPerformanceSettingUsers();
    newOne.setAppUserName(userName);
    newOne.setSettingId(settingId);
    this.saveOrUpdate(newOne);
  }

  public String getUserPosition(String userName) {
    String sql = "SELECT c.name FROM per_performance_setting_users a"
        + " LEFT JOIN `per_performance_setting` b ON a.setting_id=b.id"
        + " LEFT JOIN app_position c ON b.position_id=c.id"
        + " WHERE a.app_user_name='" + userName + "'";
    JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
    List<Map<String,Object>> list=jdbcTemplate.queryForList(sql);
    if(list.size()>0)
      return list.get(0).get("name").toString();
    else
      return "";

  }
}
